using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FrameWork.Language
{
    public class I18n
    {
        /// <summary>
        /// 语言文件引用的索引
        /// </summary>
        public static int ResourceIndex { get; set; } = 1;
        

        public static string LanguagePathTemplate { get; set; } = "FrameWork.Language;component/Languages/{0}.xaml";

        private static LanguageType _currentLanguage;
        /// <summary>
        /// 当前语言
        /// </summary>
        public static LanguageType CurrentLanguage
        {
            get
            {
                return _currentLanguage;
            }
            set
            {
                _currentLanguage = value;
                ChangeLanguage(value);
            }
        }
        /// <summary>
        /// 更换语言
        /// </summary>
        /// <param name="path"></param>
        public static void ChangeLanguage(LanguageType language)
        {
            var fileName = GetFileName(language);
            var path = string.Format(LanguagePathTemplate, fileName);
            System.Windows.Application.Current.Resources.MergedDictionaries[ResourceIndex] = new ResourceDictionary() { Source = new Uri(path, UriKind.RelativeOrAbsolute) };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string GetFileName(Enum value)
        {
            var type = value.GetType();
            var name = Enum.GetName(type, value);
            if (string.IsNullOrWhiteSpace(name))
                return value.ToString();

            var field = type.GetField(name);
            var des = field?.GetCustomAttribute<LanguageFileAttribute>();
            if (des == null)
                return value.ToString();

            return des.FileName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static string Get(string key, string flag = "lang")
        {
            return Get(key, key, flag);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultLang"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        public static string Get(string key, string defaultLang, string flag = "lang")
        {
            if (string.IsNullOrWhiteSpace(key))
                return defaultLang;

            try
            {
                if (Application.Current.Resources.MergedDictionaries.Count < ResourceIndex + 1)
                    return defaultLang;
            }
            catch
            {
                return defaultLang;
            }
            
            try
            {
                var val = Application.Current.Resources.MergedDictionaries[ResourceIndex][$"{flag}-{key}"] as string;
                if (val == null)
                    return defaultLang;
                else
                    return val;
            }
            catch
            {
                return defaultLang;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetEnum(string key)
        {
            return Get(key, key, "enum");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultLang"></param>
        /// <returns></returns>
        public static string GetEnum(string key, string defaultLang)
        {
            return Get(key, defaultLang, "enum");
        }
    }
}
